#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import tornado.web
import tornado.ioloop


class WechatHandler(tornado.web.RequestHandler):
    def data_received(self, chunk):
        pass

    def get(self):
        ret_dic = {'code': 200, 'message': 'ok', 'data': []}
        bizs = self.application.rds.smembers('wechat_biz_set')
        if bizs is None:
            ret_dic['code'] = 404
            ret_dic['message'] = 'redis公众号集合未设置'
            return ret_dic
        # sql
        # data dic list
        return ret_dic

    def post(self):
        post_data = self.request.body.decode('utf-8')
        post_args = json.loads(post_data)
        uin = post_args.get('uin')
        biz = post_args.get('biz')
        key = post_args.get('key')

        queue = f'''{biz}_queue'''
        content = f'''{uin}|{biz}|{key}'''
        self.application.rds.rpush(queue, content)
        ret_dic = {'code': 200, 'message': 'ok', 'data': ''}
        ret_str = json.dumps(ret_dic)
        self.write(ret_str)
